package com.buho29.rcmanager.bd;

import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;

public class TableDamage extends BaseActiveRecord  {

	public static final String SQL_TABLE_NAME = "damage";
	
	public static final String SQL_CREATE_TABLE = "create table " + SQL_TABLE_NAME + " (" +
				"_id INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT," +
			    "model_id INTEGER NOT NULL," +
	    		"name VARCHAR(20) NOT NULL," +
	    		"date INTEGER NULL, " +
	    		"cause INTEGER NOT NULL, " +
			    "price FLOAT NULL, " + 
			    "count INTEGER NULL " +
		    ");";
	
	public static final String COL_MODEL_ID = "model_id";
	public static final String COL_NAME = "name";
	public static final String COL_DATE = "date";
	public static final String COL_CAUSE = "cause";
	public static final String COL_PRICE = "price";
	public static final String COL_COUNT = "count";
	
	public static final String[] COLUMNS = new String[] { 
		COL_ROW_ID, COL_MODEL_ID, COL_NAME, COL_DATE ,
		COL_CAUSE,COL_PRICE , COL_COUNT
	};
	
	private long mModelId;
	public long getModelId() {return mModelId;}
	public void setModelId(long modelId) {mModelId = modelId;}

	private String mName;
	public String getName() {return mName;}
	public void setName(String name) {mName = name;}
		
	private long mDate;
	public long getDate() {return mDate;}
	
	private int mCause;
	public int getCause() {return mCause;}
	public void setCause(int cause) {mCause = cause;}
	
	private float mPrice;
	public float getPrice() {return mPrice;}
	public void setPrice(float price) {mPrice = price;}
	
	private int mCount;
	public int getCount() {return mCount;}
	public void setCount(int count) {mCount = count;}
	

	@Override
	public Boolean load() {
		Cursor cursor = mDb.query(true, SQL_TABLE_NAME,COLUMNS,
				COL_ROW_ID + "=" + mId,
				null, null,null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			loadCursor(cursor);
			cursor.close();
			return true;
		}
		return false;
	}

	@Override
	public void loadCursor(Cursor cursor) {
		mId = cursor.getLong(cursor.getColumnIndex(COL_ROW_ID));
		mModelId = cursor.getLong(cursor.getColumnIndex(COL_MODEL_ID));
		mName = cursor.getString(cursor.getColumnIndex(COL_NAME));
		mDate = cursor.getLong(cursor.getColumnIndex(COL_DATE));
		mCause = cursor.getInt(cursor.getColumnIndex(COL_CAUSE));
		mPrice = cursor.getFloat(cursor.getColumnIndex(COL_PRICE));
		mCount = cursor.getInt(cursor.getColumnIndex(COL_COUNT));
	}
	
	@Override
	public long save() {
		ContentValues values = new ContentValues();
		values.put(COL_MODEL_ID,mModelId);
		values.put(COL_NAME,mName);
		values.put(COL_CAUSE,mCause);
		values.put(COL_PRICE,mPrice);
		values.put(COL_COUNT,mCount);
		
		if (mId < 1) {
			values.put(COL_DATE,new Date().getTime());
			return mDb.insert(SQL_TABLE_NAME, null, values);
		} else {
			values.put(COL_DATE,mDate);
			return mDb.update(SQL_TABLE_NAME, values, COL_ROW_ID      + "=" + mId, null);
		}  
	}
	
	@Override
	public boolean delete() {
		return mDb.delete(SQL_TABLE_NAME, COL_ROW_ID + "=" + mId,     null) > 0;
	}
	
	@Override
	public Cursor retrieveAll() {
		return mDb.query(SQL_TABLE_NAME, COLUMNS, null, null,
				null, null, null);
	}
	
	public Cursor fetchModel(long modelId,String orderBy) {
		return mDb.rawQuery(
				"SELECT * , (price * count) AS total " +
				"FROM damage WHERE model_id=? ORDER BY "+orderBy+" DESC",
				new String[]{String.valueOf(modelId)}
			);
	}
	
	public String[] getListNames(long modelId) {
	    
	    Cursor tmp = mDb.rawQuery(
				"SELECT DISTINCT name " +
				"FROM damage WHERE model_id=? ORDER BY "+COL_NAME,
				new String[]{String.valueOf(modelId)}
			);
	    
	    int count = tmp.getCount();
	    String[] result = new String[count];
	    
		for (int i = 0; i < count; i++) {
			tmp.moveToPosition(i);
			result[i] = tmp.getString(tmp.getColumnIndex(COL_NAME));
		}
		tmp.close();
		return result;	
	}
	
	public boolean deleteModel(long modelID) {
		return mDb.delete(SQL_TABLE_NAME, COL_MODEL_ID + "=" + modelID,     null) > 0;
	}
	
}
